﻿package com.example.goukakuproject;

import com.NegativeDrawerHelper.NsMenuAdapter;
import com.NegativeDrawerHelper.NsMenuItemModel;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


public class GKStudylfeftmenu extends TabActivity {
	private int bai = 1;
	private int trinhDo = 0;
	private ListView mDrawerList;
	private DrawerLayout mDrawer;
	private CustomActionBarDrawerToggle mDrawerToggle;
	private String[] menuItems;
	TabSpec vocabspec, kanjispec,grammarspec,searchspec,quizspec; 	
	TabHost tabHost;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_gkstudylfeftmenu);
		Intent callerIntent=getIntent();
		Bundle packageFromCaller = callerIntent.getBundleExtra("Main");
// Trả về trình độ N2,3,4,5		
		trinhDo = packageFromCaller.getInt("N");  
        this.setTitle("Trình độ N"+trinhDo + ". Bai " + bai);
        Bundle bundle=new Bundle();		 			 
		bundle.putInt("bai", bai);
		bundle.putInt("trinhdo", trinhDo);
        
        tabHost = getTabHost();		
		vocabspec = tabHost.newTabSpec("Tuvung");
		vocabspec.setIndicator("Từ vựng",
				getResources().getDrawable(R.drawable.ic_launcher));
		Intent vocabIntent = new Intent(GKStudylfeftmenu.this, GKVocabtab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);				
		vocabIntent.putExtra("VocabIntent", bundle);
		vocabspec.setContent(vocabIntent);
		
		kanjispec = tabHost.newTabSpec("ChuHan");
		kanjispec.setIndicator("Chữ hán",
				getResources().getDrawable(R.drawable.ic_launcher));
		Intent kanjiIntent = new Intent(GKStudylfeftmenu.this, GKKanjitab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		kanjiIntent.putExtra("KanjiIntent", bundle);
		kanjispec.setContent(kanjiIntent);
		
		grammarspec = tabHost.newTabSpec("NguPhap");
		grammarspec.setIndicator("Ngữ pháp",
				getResources().getDrawable(R.drawable.ic_launcher));
		Intent grammarIntent = new Intent(GKStudylfeftmenu.this, GKGrammartab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		grammarIntent.putExtra("GrammarIntent", bundle);
		grammarspec.setContent(grammarIntent);
		
		searchspec = tabHost.newTabSpec("Timkiem");
		searchspec.setIndicator("Tìm kiếm",
				getResources().getDrawable(R.drawable.ic_launcher));
		Intent searchIntent = new Intent(GKStudylfeftmenu.this, GKSearchtab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		searchIntent.putExtra("SearchIntent", bundle);
		searchspec.setContent(searchIntent);
		
		quizspec = tabHost.newTabSpec("Kiemtra");
		quizspec.setIndicator("Kiểm tra",
				getResources().getDrawable(R.drawable.ic_launcher));
		Intent quizIntent = new Intent(GKStudylfeftmenu.this, GKQuizsettingtab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		quizIntent.putExtra("QuizIntent", bundle);
		quizspec.setContent(quizIntent);		
		
		tabHost.addTab(vocabspec); 
		tabHost.addTab(kanjispec); 
		tabHost.addTab(grammarspec); 
		tabHost.addTab(searchspec); 
		tabHost.addTab(quizspec); 
						
		
		// enable ActionBar app icon to behave as action to toggle nav drawer
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

		// set a custom shadow that overlays the main content when the drawer
		// opens
		mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

		_initMenu();
		mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
		mDrawer.setDrawerListener(mDrawerToggle);

	}

	private void _initMenu() {
		NsMenuAdapter mAdapter = new NsMenuAdapter(this);
		mAdapter.addHeader("Bai hoc");		
		for (int i = 0; i < 10; i++) {
			NsMenuItemModel mItem = new NsMenuItemModel("Bai " + (i+1), R.drawable.ic_launcher);
			mAdapter.addItem(mItem);
		}
		//mAdapter.addHeader("Bottom");
		mDrawerList = (ListView) findViewById(R.id.drawer);
		if (mDrawerList != null)
			mDrawerList.setAdapter(mAdapter);		 
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.gkstudylfeftmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		/*
		 * The action bar home/up should open or close the drawer.
		 * ActionBarDrawerToggle will take care of this.
		 */
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}

		// Handle your other action bar items...
		return super.onOptionsItemSelected(item);
	}

	private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

		public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
			super(
			    mActivity,
			    mDrawerLayout, 
			    R.drawable.ic_drawer,
			    R.string.ns_menu_open, 
			    R.string.ns_menu_close);
		}

		@Override
		public void onDrawerClosed(View view) {
			//getActionBar().setTitle(getString(R.string.ns_menu_close));
			invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
		}

		@Override
		public void onDrawerOpened(View drawerView) {
			//getActionBar().setTitle(getString(R.string.ns_menu_open));
			invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
		}
	}
	
	private class DrawerItemClickListener implements ListView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// Highlight the selected item, update the title, and close the drawer
			// update selected item and title, then close the drawer
	        mDrawerList.setItemChecked(position, true);	        
	        bai = position;
	        Bundle bundle=new Bundle();
	        bundle.putInt("trinhdo", trinhDo);
			bundle.putInt("bai", bai);
	        tabHost.clearAllTabs();
			getActionBar().setTitle("Trình độ N"+ trinhDo + ".Bài " + bai);	  	        
	        
	        Intent i = new Intent(GKStudylfeftmenu.this, GKVocabtab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);	        
			i.putExtra("VocabIntent", bundle);	        
	        vocabspec.setContent(i);	        
	        tabHost.addTab(vocabspec);
	        
	        i = new Intent(GKStudylfeftmenu.this, GKKanjitab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);	        
			i.putExtra("KanjiIntent", bundle);	        
	        kanjispec.setContent(i);	        	        
	        tabHost.addTab(kanjispec);
	        
	        i = new Intent(GKStudylfeftmenu.this, GKGrammartab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);	        
			i.putExtra("GrammarIntent", bundle);	        
	        grammarspec.setContent(i);	        	        
	        tabHost.addTab(grammarspec);
	        
	        i = new Intent(GKStudylfeftmenu.this, GKSearchtab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);	        
			i.putExtra("SearchIntent", bundle);	        
	        searchspec.setContent(i);	        	        
	        tabHost.addTab(searchspec);
	        
	        i = new Intent(GKStudylfeftmenu.this, GKQuizsettingtab.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);	        
			i.putExtra("QuizIntent", bundle);	        
	        quizspec.setContent(i);	        	        
	        tabHost.addTab(quizspec);
	        
	        mDrawer.closeDrawer(mDrawerList);
			
		}		
	}		
}
